<?php
/**
 * Created by PhpStorm.
 * User: 程阳
 * Date: 2017/3/5
 * Time: 10:33
 */

namespace Home\Controller;


use Common\Controller\HomeController;

class OrderController extends HomeController
{
    public function index(){
        if(!isset($_SESSION['home']['user'])){
            $this->error('请先登录',u('home/user/login'));die;
        }
        $uid = $_SESSION['home']['user']['uid'];
        $address = M("Uaddress")->where("uid=$uid")->select();
//        dd($address);
        $this->assign("address",$address);
        $goodsData = $this->choosed();
        if($goodsData){
//                dd($goodsData);
//            dd($_SESSION);
            $this->assign('goodsData',$goodsData);
        }
        $this->display();
    }

    public function getGoods(){
        $goodKey = explode(',',rtrim(I("post.goodKey"),','));
        if(!empty($goodKey)){
            $_SESSION['goodKey'] = $goodKey;
            $this->ajaxReturn(['volid'=>true]);
        }else{
            $this->ajaxReturn(['volid'=>false]);
        }

    }

    public function confirmOrder(){
        if(IS_AJAX){
//            dd(I("post."));
            $data['ocode'] = '';
            for($i = 0;$i < 11;$i++){
                $data['ocode'] .= rand(0,9);
            }
            // 向订单表里添加数据
            $data['oname'] = I("post.uaname");
            $data['oaddress'] = I("post.uarea") . " " .  I("post.uadetail");
            $data['oprice'] = I("post.total");
            $data['octime'] = date("Y-m-d H:i:s",time());
            $data['remark'] = '无';
            $data['ostatus'] = '1'; // 订单状态默认为1，表示未发货
            $data['uid'] = $_SESSION['home']['user']['uid'];
//            dd($data);die;
            $res = M("Order")->add($data);
            // 向订单列表里添加数据
            if(!$res){
                $this->ajaxReturn(["status"=>false]);die;
            }
            // 获得要结算的商品数据
            $goodsData = $this->choosed();
            foreach($goodsData['good'] as $k => $v){
                $data['olnums'] = $v['num'];
                $data['olprice'] = $v['price'];
                $data['info'] = $v['info'];
                $data['olremark'] = '无';
                $data['oid'] = $res;
                $data['gid'] = $v['id'];
                M("OrderList")->add($data);
            }
            $this->ajaxReturn(["status"=>true]);die;
        }
    }

    // 通过购物车里选择结算的商品sid取得session中对应的数据
    protected function choosed(){
        $goodsData = [];
        foreach($_SESSION['goodKey'] as $k => $v){
            $goodsData['good'][$v] = $_SESSION['cart']['goods'][$v];
        }
        $total = 0;
        $total_rows = 0;
        foreach($goodsData['good'] as $k => $v){
            $goodsData['good'][$k]['good'] = M("Goods")->where("gid = {$v['id']}")->find();
            $goodsData['good'][$k]['info'] = $v['name'];
            foreach($v['options'] as $kk => $vv){
                $goodsData['good'][$k]['info'] .= $vv;
            }
            $total += intval($v['total']);
            $total_rows += floatval($v['num']);
        }
        $goodsData['total'] = $total;
        $goodsData['total_rows'] = $total_rows;
        return $goodsData;
    }
}